using System;
using System.IO;
using System.Web.UI;
using THANHTUNGO.Portal.Components.Security;
using THANHTUNGO.Portal.Components.Settings;

namespace THANHTUNGO.Portal.Components.UI
{
    public class CachedPortalModuleControl : Control
    {
        // Private field variables

        private Module _Module;
        private string _cachedOutput = "";
        private int _portalId = 0;


        // Public property accessors

        public Module ModuleConfiguration
        {
            get { return _Module; }
            set { _Module = value; }
        }

        public int ModuleID
        {
            get { return _Module.ID; }
        }

        public int PortalId
        {
            get { return _portalId; }
            set { _portalId = value; }
        }

        //*********************************************************************
        //
        // CacheKey Property
        //
        // The CacheKey property is used to calculate a "unique" cache key
        // entry to be used to store/retrieve the portal module's content
        // from the ASP.NET Cache.
        //
        //*********************************************************************

        public string CacheKey
        {
            get { return "Key:" + this.GetType() + this.ModuleID + PortalSecurity.IsInRoles(_Module.AuthorizedEditRoles); }
        }

        //*********************************************************************
        //
        // CreateChildControls Method
        //
        // The CreateChildControls method is called when the ASP.NET Page Framework
        // determines that it is time to instantiate a server control.
        // 
        // The CachedPortalModuleControl control overrides this method and attempts
        // to resolve any previously cached output of the portal module from the 
        // ASP.NET cache.  
        //
        // If it doesn't find cached output from a previous request, then the
        // CachedPortalModuleControl will instantiate and add the portal module's
        // User Control instance into the page tree.
        //
        //*********************************************************************

        protected override void CreateChildControls()
        {
            // Attempt to resolve previously cached content from the ASP.NET Cache

            if (_Module.CacheTimeOut > 0)
            {
                _cachedOutput = (string) Context.Cache[CacheKey];
            }

            // If no cached content is found, then instantiate and add the portal
            // module user control into the portal's page server control tree

            if (_cachedOutput == null)
            {
                base.CreateChildControls();

                PortalModuleControl module = (PortalModuleControl) Page.LoadControl(_Module.SourceFile);

                module.Module = this.ModuleConfiguration;
                module.PortalID = this.PortalId;

                this.Controls.Add(module);
            }
        }

        //*********************************************************************
        //
        // Render Method
        //
        // The Render method is called when the ASP.NET Page Framework
        // determines that it is time to render content into the page output stream.
        // 
        // The CachedPortalModuleControl control overrides this method and captures
        // the output generated by the portal module user control.  It then 
        // adds this content into the ASP.NET Cache for future requests.
        //
        //*********************************************************************

        protected override void Render(HtmlTextWriter output)
        {
            // If no caching is specified, render the child tree and return 

            if (_Module.CacheTimeOut == 0)
            {
                base.Render(output);
                return;
            }

            // If no cached output was found from a previous request, render
            // child controls into a TextWriter, and then cache the results
            // in the ASP.NET Cache for future requests.

            if (_cachedOutput == null)
            {
                TextWriter tempWriter = new StringWriter();
                base.Render(new HtmlTextWriter(tempWriter));
                _cachedOutput = tempWriter.ToString();

                Context.Cache.Insert(CacheKey, _cachedOutput, null, DateTime.Now.AddSeconds(_Module.CacheTimeOut), TimeSpan.Zero);
            }

            // Output the user control's content

            output.Write(_cachedOutput);
        }

        //-----------------------------------------------------------------------------------------
    }
}